package com.tdam2011.grupo04.SMSWeb;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.parsers.SAXParser;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.xml.sax.SAXException;

public class ComunicacionServer {
	private String usuario;
	private String contrasenia;
	
	static final int COM_LOGIN= 0;
	static final int COM_ENVIAR= 1;
	static final int COM_CONSULTAR= 2;
	
	private String uri= "http://192.168.1.34:8080/MessageSender/Sender";
	public static boolean isOnline= true;
	
	private Handler handler;
		
	public ComunicacionServer(String usuario, String contrasenia){
		this.usuario= usuario;
		this.contrasenia= contrasenia;
	}
	
	public Handler getHandler(){
		return handler;
	}

	public String getXML(int tipoComunicacion, String destino, String mensaje, String fechaUltimaConsulta){
		String tipo= "", detalle= "";
		switch (tipoComunicacion) {
			case COM_LOGIN:
				tipo= "register-user";
				detalle= "<user username='" + usuario + "' password='" + contrasenia +"'/>";
				break;
			case COM_ENVIAR:
				tipo= "send-message";
				detalle= "<auth username='" + usuario + "' key='" + contrasenia + "'></auth>" +
						 "<message to='"+ destino +"'><![CDATA["+ mensaje +"]]></message>";
				break;
			case COM_CONSULTAR:
				tipo= "get-messages";
				detalle= "<auth username='" + usuario +"' key='" + contrasenia + "'></auth>" +
						 "<filter type='timestamp'>" + fechaUltimaConsulta + "</filter>";
				break;
		}
		String xml= "<action id='REQUEST_RANDOM_VALUE' name='" + tipo + "'><action-detail>";
		xml+= detalle;
		xml+= "</action-detail></action>";
		
		return xml;
	}
	
	public boolean parsear(String xml, int tipoMensaje) throws ClientProtocolException, IOException, SAXException{
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(uri);
		BasicHttpEntity ent = new BasicHttpEntity();
		ent.setContentType("application/xml");
		
		ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes());
		ent.setContent(in);
		post.setEntity(ent);						
		HttpResponse responsePost= client.execute(post);
		HttpEntity resEntity= responsePost.getEntity();
								
		if(resEntity == null) return false;
		
		SAXParser parser = XmlParserProvider.getParser();
		if(tipoMensaje == COM_LOGIN) handler= new RegistroUsuarioHandler();
		else if(tipoMensaje == COM_ENVIAR) handler= new EnvioMensajeHandler();
		else handler= new ConsultaMensajeHandler();
			
		String res= EntityUtils.toString(resEntity);
		parser.parse(new ByteArrayInputStream(res.getBytes()), handler);
		XmlParserProvider.releaseParser(parser);
			
		return handler.getIsOK();
	}
}
